/*
 *                          许可协议    
 *    
 *  COPYRIGHT ©2008 The Siox software project Lab.
 * 
 * 版权声明:                               
 *  1.任何"派生软件"都必须包含此许可协议,"派生软件的开发者"必   
 *  须完全保留被继承"软件"的许可协议和"版权信息".          
 *  2."派生软件开发者"在别人使用他所"派生"的"软件"时,自动成为"开
 *  发者".                               
 *  3."使用者"所生成的二进制代码文件须包含"原始开发者"的版权信息. 
 *  4.如该软件并未得到"开发者"的开源，那么"使用者"对本程序仅有使  
 *  用权，并不得进行反向工程.                      
 *  5.本协议保留"原始开发者"的版权信息在许可协议文本之内.
 *  6.本文档作为源代码/程序的基本协议,如有变化作为本协议扩充 
 *                                     
 * 免责声明:                               
 *  1.使用本"软件"都意味"使用者"是自愿使用本"软件",接受此许可协 
 *  议的所有条款.如果"使用者"不同意此许可协议就不能使用本"软件".  
 *  2.本"软件"虽然在开发者的开发环境下经过严格测试,但不能排除    
 *  "软件"中仍然存在错误.由于使用本"软件"所造成的任何损失,"开发  
 *  者"不负任何责任!                          
 *  3.本"软件"在被"使用"过程中,由"使用者"发现的任何错误必须再反 
 *  馈给"开发者"并经过"开发者"同意后,才可对其他人公开.任何未经过  
 *  "开发者"同意而被使用者擅自公开的本"软件"的错误,"开发者"有权  
 *  要求其对"开发者"名誉损失负法律责任. 
 * 
 *  个人主页:http://hi.baidu.com/lindily
 *  电子信箱:lindily@126.com               
 */

package cn.siox.util;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.util.*;

/**
 * @类名 DateChooser
 * @作者 Lindily
 * @日期 2008-11-20 17:22:30
 */
 /**
  *  [Test.java]
  *
  *  测试类
  *
  * 创建日期：(2003-10-26)
  * @author：ONE_Fox
  * @author：ONE_Fox@163.com
  */


 public class DateChooser extends javax.swing.JDialog {

    private Calendar calendar;
    private Calendar retCal;

    public DateChooser() {
        super();
        init();
    }

    public DateChooser(Dialog owner) {
        super(owner);
        init();
    }

    public DateChooser(Dialog owner, boolean modal) {
        super(owner, modal);
        init();
    }

    public DateChooser(Dialog owner, String title) {
        super(owner, title);
        init();
    }

    public DateChooser(Dialog owner, String title, boolean modal) {
        super(owner, title, modal);
        init();
    }

    public DateChooser(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) {
        super(owner, title, modal, gc);
        init();
    }

    public DateChooser(Frame owner) {
        super(owner);
        init();
    }

    public DateChooser(Frame owner, boolean modal) {
        super(owner, modal);
        init();
    }

    public DateChooser(Frame owner, String title) {
        super(owner, title);
        init();
    }

    public DateChooser(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        init();
    }

    public DateChooser(Frame owner, String title, boolean modal, GraphicsConfiguration gc) {
        super(owner, title, modal, gc);
        init();
    }

    private void init() {
        initComponents();
        //init date
        calendar = Calendar.getInstance();
        int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH);
        jComboBox1.setSelectedIndex(m);
        jSpinner1.setValue(calendar.getTime());
        monthCalendar.setYearMonth(y, m+1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        basePanel = new javax.swing.JPanel();
        containerPanel = new javax.swing.JPanel();
        controlPanel = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jSpinner1 = new javax.swing.JSpinner();
        viewPanel = new javax.swing.JPanel();
        monthCalendar = new MonthlyCalendar() {
            protected void dateSelected() {
                doSelection();
            }
        };

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Date Chooser");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        basePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 10));

        basePanel.setBackground(new java.awt.Color(252, 250, 252));
        containerPanel.setLayout(new java.awt.BorderLayout());

        containerPanel.setBorder(new javax.swing.border.TitledBorder("Date"));
        containerPanel.setOpaque(false);
        controlPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 12, 0));

        controlPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(5, 0, 10, 0)));
        controlPanel.setOpaque(false);
        jComboBox1.setFont(new java.awt.Font("宋体", 0, 11));
        jComboBox1.setMaximumRowCount(7);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        controlPanel.add(jComboBox1);

        jSpinner1.setFont(new java.awt.Font("宋体", 0, 11));
        jSpinner1.setModel(new javax.swing.SpinnerDateModel());
        jSpinner1.setEditor(new javax.swing.JSpinner.DateEditor(jSpinner1, "yyyy"));
        jSpinner1.setPreferredSize(jComboBox1.getPreferredSize());
        jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner1StateChanged(evt);
            }
        });

        controlPanel.add(jSpinner1);

        containerPanel.add(controlPanel, java.awt.BorderLayout.NORTH);

        viewPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));

        viewPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(0, 0, 10, 0)));
        viewPanel.setOpaque(false);
        monthCalendar.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
        viewPanel.add(monthCalendar);

        containerPanel.add(viewPanel, java.awt.BorderLayout.CENTER);

        basePanel.add(containerPanel);

        getContentPane().add(basePanel, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
        // TODO add your handling code here:
        Date date = (Date) jSpinner1.getValue();
        calendar.setTime(date);
        monthCalendar.setYear(calendar.get(Calendar.YEAR));
        monthCalendar.repaint();
    }//GEN-LAST:event_jSpinner1StateChanged

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == java.awt.event.ItemEvent.DESELECTED)
            return;
        monthCalendar.setMonth(jComboBox1.getSelectedIndex()+1);
        monthCalendar.repaint();
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        retCal = null;
    }//GEN-LAST:event_formWindowClosing

    /** Override parent setVisible method to include resetting of highlighted date */
    public void setVisible(boolean b) {
        monthCalendar.resetHighlight();
        super.setVisible(b);
    }

    /** Contains handling code for date selection event */
    private void doSelection() {
        retCal = monthCalendar.getSelectedDate();
        super.setVisible(false);
        dispose();
    }

    /**
     * User code can call this method to get the selected date as a Calendar object.
     * If the DateChooser dialog is closed by clicking the "Close" button, this
     * method returns null;
     * Else, the DateChooser dialog is closed by selecting a date, and the method
     * will return a Calendar object representing that date.
     */
    public Calendar getSelectedDate() {
        return retCal;
    }

    /**
     * Set the calendar year and month to the specified value.
     * Should be called before set the date chooser visible.
     */
    public void setYearMonth(int y, int m) {
        if (y < 1970) {
            throw new IllegalArgumentException("必须是1970年后.");
        }
        if (m < 1 || m > 12) {
            throw new IllegalArgumentException("月份非法.");
        }
        jComboBox1.setSelectedIndex(m-1);
        calendar.set(Calendar.YEAR, y);
        jSpinner1.setValue(calendar.getTime());
        monthCalendar.setYearMonth(y, m);
    }

    /**
     * Bean method to set start day of a week.
     * Will delegate to MonthlyCalendar.setWeekStartOnSunday(boolean) method.
     */
    public void setWeekStartOnSunday(boolean b) {
        monthCalendar.setWeekStartOnSunday(b);
    }

    /**
     * Bean method to get start day of a week.
     * Will delegate to MonthlyCalendar.getWeekStartOnSunday method.
     */
    public boolean getWeekStartOnSunday() {
        return monthCalendar.getWeekStartOnSunday();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel basePanel;
    private javax.swing.JPanel containerPanel;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JSpinner jSpinner1;
    private MonthlyCalendar monthCalendar;
    private javax.swing.JPanel viewPanel;
    // End of variables declaration//GEN-END:variables

}